package com.bw.common.mvvm.viewmodel;

import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.common.entity.LiveDataEntity;
import com.bw.common.entity.NetErrorEntity;
import com.bw.common.livedatabus.LiveDataBus;
import com.bw.common.mvvm.model.BaseModel;
import com.bw.network_sdk.entity.BaseEntity;

import java.lang.reflect.ParameterizedType;
import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BaseViewModel<M extends BaseModel> extends ViewModel implements IViewModel, Observer<BaseEntity> {

    protected M mModel;
    private CompositeDisposable compositeDisposable = null;
    protected LiveDataViewModel liveDataViewModel = null;

    public LiveDataViewModel getLiveDataViewModel() {
        return liveDataViewModel;
    }

    public BaseViewModel() {
        initModle();
        liveDataViewModel = new LiveDataViewModel();
        compositeDisposable = new CompositeDisposable();
    }

    private void initModle() {
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        Class clazz = (Class) type.getActualTypeArguments()[0];
        try {
            mModel = (M) clazz.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCreate() {
        initData();
    }

    protected abstract void initData();

    @Override
    public void onDestroy() {
        compositeDisposable.dispose();
        compositeDisposable.clear();
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {
        compositeDisposable.add(d);
    }

    @Override
    public void onNext(@NonNull BaseEntity entity) {
        //根据返回值BaseEntity 进行类型判断->是否为具体出错类型
        if (entity instanceof NetErrorEntity){
            NetErrorEntity netErroEntity = (NetErrorEntity) entity;
            ToastUtils.showShort(netErroEntity.message);
            return;
        }
        onRestle(entity);
    }

    protected abstract void onRestle(BaseEntity entity);

    @Override
    public void onError(@NonNull Throwable e) {
        e.printStackTrace();
        LogUtils.e("onError:"+e.getMessage());
        ToastUtils.showShort("请求错误");
    }

    @Override
    public void onComplete() {
        hideDialog();
    }

    //显示dialog
    protected void showDialog(){
    }
    //关闭dialog
    protected void hideDialog(){
    }

    public static class LiveDataViewModel{
        private LiveDataBus<LiveDataEntity> uiData=new LiveDataBus<>();

        public LiveDataBus<LiveDataEntity> getUiData() {
            return uiData;
        }

        public void post(LiveDataEntity entity){
            uiData.postValue(entity);
        }
    }

}
